package com.example.yoho.utils;

import com.example.lib_core.network.Api;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class NetUtils {
    private NetUtils(){}

    private static NetUtils netUtils=null;

    public static NetUtils getInstance(){
        if (netUtils==null){
            netUtils=new NetUtils();
        }
        return netUtils;
    }
    private Retrofit retrofit;

    public Retrofit getRetrofit() {
        if (retrofit==null){
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            //缓存拦截器
            CatchInterceptor catchInterceptor = new CatchInterceptor();
            builder.addInterceptor(interceptor);//请求拦截器
            builder.addInterceptor(catchInterceptor);//缓存拦截器
            Retrofit.Builder okbuilder = new Retrofit.Builder();
            okbuilder.baseUrl(Api.HOME_URL);
            okbuilder.addConverterFactory(GsonConverterFactory.create());
            okbuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            retrofit=okbuilder.build();
        }
        return retrofit;
    }
}
